
import H5Websocket from "@/net/Net1";

let lastTouchEnd = 0; //更新手指弹起的时间
document.documentElement.addEventListener("touchstart", function (event) {
  //多根手指同时按下屏幕，禁止默认行为
  if (event.touches.length > 1) {
    event.preventDefault();
  }
}, false);

document.documentElement.addEventListener(
  "touchend",
  function (event) {
    let now = new Date().getTime();
    if (now - lastTouchEnd <= 300) {
      //当两次手指弹起的时间小于300毫秒，认为双击屏幕行为
      event.preventDefault();
    } else {
      // 否则重新手指弹起的时间
      lastTouchEnd = now;
    }
  },
  false
);
//阻止双指放大页面
document.documentElement.addEventListener("gesturestart", function (event) {
  event.preventDefault();
});
document.oncontextmenu = function (e) {
  e.preventDefault()
}
let deferredPrompt: any;
window.addEventListener('beforeinstallprompt', (e) => {
  e.preventDefault();
  deferredPrompt = e;
  // 显示安装按钮
});
// 在用户点击安装按钮时调用
window.addEventListener('click', () => {
  const appinstall_popup = sessionStorage.getItem('appinstall_popup')
  if (appinstall_popup || !deferredPrompt) return
  deferredPrompt.prompt();
  deferredPrompt.userChoice.then((_choiceResult: any) => {
    // if (choiceResult.outcome === 'accepted') {
    //   console.log('User accepted the install prompt');
    // } else {
    //   console.log('User dismissed the install prompt');
    // }
    sessionStorage.setItem('appinstall_popup', '1')
    deferredPrompt = null;
  });
});

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js')
    .then((registration) => {
      console.log('Service Worker registered with scope:', registration.scope);
    })
    .catch((error) => {
      console.log('Service Worker registration failed:', error);
    });
}
// window.addEventListener('offline',function(){
//   alert('Please check if your network connection is normal')
// },true)

document.addEventListener("visibilitychange", async () => {
  if (document.visibilityState == "visible") {
    console.log('instance===>', H5Websocket.instance);

    if (H5Websocket.instance == null) {
      // window.location.reload();
    }
    console.log("进入前台");
  }
  if (document.visibilityState == "hidden") {
    // H5Websocket.close()
    console.log("切换到后台")
  }
})


let startX = 0;
let startY = 0;

document.addEventListener('touchstart', (e) => {
  startX = e.touches[0].clientX;
  startY = e.touches[0].clientY;
}, { passive: false });

document.addEventListener('touchmove', (e) => {
  const deltaX = e.touches[0].clientX - startX;
  const deltaY = e.touches[0].clientY - startY;

  // 判断滑动是否是横向滑动
  if (Math.abs(deltaX) > Math.abs(deltaY)) {
    e.preventDefault(); // 阻止默认行为（左右滑动切换路由）
  }
}, { passive: false });
export const stopMove = () => {
  if (window.scrollY > 0) {
    window.scrollTo(0, 0); // 强制返回顶部
  }
}
document.addEventListener('scroll', stopMove);
function setFullHeight() {

  const height = (window.visualViewport ? window.visualViewport.height : window.innerHeight);
  document.documentElement.style.setProperty('--vh', `${height * 0.01}px`);
  document.documentElement.style.setProperty('--app-height', `${height}px`);
}

window.addEventListener('load', setFullHeight);
window.addEventListener('resize', setFullHeight);